angular.module('meIntegratListControllers', [])

  .controller('IntegratListCtrl', function ($scope, $state, IntegratList, Storage, ENV, ConvertedMsg) {

    $scope.ENV = ENV;
    $scope.isShowProductDetail = false;
    $scope.isShowPopup = false;
    var memberId = '';
    //进入积分商城界面，进行数据加载
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      IntegratList.getIntegratList();
    });
    $scope.$on('Me.getIntegratList', function () {
      var result = IntegratList.getIntegratListJson();
      //将数据绑定到界面上
      $scope.productList = result.productList;
    });
    //获取商品的相关信息，放置在弹出框中
    $scope.showProduct = function (index) {
      var product = $scope.productList[index];
      $scope.productName = product.psp_name;
      $scope.productImg = product.psp_img_url;
      $scope.productPrice = product.psp_price;
      $scope.productPoint = product.psp_point;
      $scope.productId = product.psp_id;
      $scope.isShowProductDetail = true;

    };

    $scope.closePopup = function () {
      $scope.isShowProductDetail = false;
    };

    // 访问后台获取是否可以进行兑换
    $scope.gotoExchange = function (productId) {
      //将商品编号赋给converted-msg的service
      ConvertedMsg.setProductId(productId);
      IntegratList.getExchangeResult(productId,memberId);
    };

    // 获取兑换判断结果
    $scope.$on('Me.getExchangeResult', function () {
      var result = IntegratList.getExchangeResultJson();
      if (result.pointStatus) {
        //可以兑换
        $scope.isShowProductDetail = false;

        $state.go('converted-msg');
      } else {
        //不可以兑换
        $scope.currentPoint = '您当前积分为：' + result.memberPoint;
        $scope.goodPoint = '商品所需积分：' + result.productPoint;
        $scope.resultMsg = result.pointMsg;
        $scope.isShowPopup = true;
      }
    });

    //关闭弹窗
    $scope.closeMsgPopup = function () {
      $scope.isShowPopup = false;
    };

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };



  });
